<template>
  <div class="detailbox border">
    <div class="flexbox">
      <div class="shrinkbox_v2">
        角色选择
      </div>
      <div class="contentbox_v2">
        <el-select v-if="!feildConfig.isReadonly" v-model="editData.upRoleId" 
        class="diyselect" placeholder="请选择角色" @change="changeUpRole">
          <el-option
            v-for="(item, index) in upRoles"
            :key="index"
            :label="item.roleName"
            :value="item.id">
          </el-option>
          <el-option
            label="铺手"
            :value="9">
          </el-option>
        </el-select>
        <div v-else class="div_input">
          {{ editData.upRoleId }}
        </div>
      </div>
      <div class="shrinkbox_v2">
        用户选择
      </div>
      <div class="contentbox_v2" style="flex:2;">
        <el-select v-if="!feildConfig.isReadonly" v-model="editData.userId" 
        class="diyselect" filterable placeholder="请选择用户">
        <!--  -->
          <el-option
            :label="$utils.cookies.get('nickname')?$utils.cookies.get('nickname'):$utils.cookies.get('userName')"
            :value="$utils.cookies.get('userId')">
          </el-option>
          <el-option
            v-for="(item, index) in userList"
            :key="index"
            :label="item.nickName"
            :value="item.id">
          </el-option>
        </el-select>
        <div v-else class="div_input">
          {{ editData.styleNo }}
        </div>
      </div>
    </div>

  </div>
</template>

<script setup>
  import { ref, inject, defineEmits } from 'vue'
  // import { useStore } from 'vuex'
  // 注入全局utils工具js
  const $utils = inject('$utils')
  const editData = ref({})
  const feildConfig = ref({
    isReadonly: false
  })

  // 定义emit方法
  const emit = defineEmits()

  const upRoles = ref([])
  const userList = ref([])

  function initData(obj) {
    editData.value = obj.data ? obj.data : {}
    feildConfig.value = obj.feildConfig ? obj.feildConfig : feildConfig.value
    getRoleList()
  }

  function getRoleList () {
    $utils.post({
      url: '/Api/User/GetRoleList',
      showLoading: true,
      ok: (res) => {
        upRoles.value = res.data
      }
    })
  }
  function changeUpRole (e) {
    getUserByRoleId(e)
  }
  function getUserByRoleId (id) {
    $utils.post({
      url: '/Api/User/GetList',
      params: {
        roleId: id
      },
      showLoading: true,
      ok: (res) => {
        userList.value = res.data
      }
    })
  }





  function submitData() {
    console.log('提交数据')
    console.log(editData.value)
    if (!editData.value.userId) {
      $utils.message({
        message: '请选择用户',
        type: 'danger'
      })
      return
    }
    $utils.post({
      url: '/Api/Device/Bind',
      params: editData.value,
      showLoading: true,
      ok: (res) => {
        $utils.message({
          message: '绑定成功',
          type: 'success'
        })
        emit('close-dialog', false)
      }
    })
    // emit('close-dialog', false)
  }

  defineExpose({
    initData,
    submitData
  })

</script>

<style scoped>
.saveBtn {
  background-color: #4999FA;
  color: #fff;
  font-weight: normal;
  font-size: 14px;
  padding: 3px 10px;
  border-radius: 3px;
  margin-right: 10px;
}
.saveBtn:hover {
  /* background-color: #7BB8FB; */
  opacity: 0.8;
  cursor: pointer;
}
.split_title {
  background-color: #EBEEF5;
  font-size: 16px;
  height: 30px;
  align-items: center;
  font-family: '楷体';
  font-weight: bold;
  display: flex;
  position: relative;
}
.split_title_content {
  flex-grow: 1;
  text-align: center;
}
.split_title_btn {
  display: flex;
  position: absolute;
  top: 0;
  right: 0;
  height: 100%;
  align-items: center;
}
.shrinkbox_v2 {
  width: 70px;
}
  .diyinput :deep(.el-input__wrapper){
    border: 0 !important;
    outline: none;
    box-shadow: none;
    font-size: 12px;
  }
.diyselect :deep(.el-input__wrapper){
  border: 0 !important;
  outline: none;
  box-shadow: none;
  font-size: 12px;
  /* pointer-events: none; */
}
.diyselect :deep(.el-input__wrapper:hover){
  border: 0 !important;
  outline: none;
  box-shadow: none;
}
.diyselect :deep(.el-input__wrapper:active){
  border: 0 !important;
  outline: none;
  box-shadow: none;
}
.diyselect :deep(.el-select__wrapper) {
  box-shadow: unset !important;
}
.diyinput :deep(.el-textarea__inner){
  border: 0 !important;
  outline: none;
  box-shadow: none;
  font-size: 12px;
}
.diydatepicker :deep(.el-input__wrapper){
  border: 0 !important;
  outline: none;
  box-shadow: none;
  font-size: 12px;
}
.diyformitem {
  margin-bottom: 0;
}
.diydatapicker :deep(.el-date-editor){
  width: 100%;
}
.diydatapicker :deep(.el-input__wrapper){
  border: 0 !important;
  outline: none;
  box-shadow: none;
}
.diyinput :deep(.el-input-group__append){
  border: 0 !important;
  outline: none;
  background-color: unset;
  box-shadow: none;
  padding: 0 10px 0 5px;
  font-weight: bold;
  font-size: 16px;
}
/deep/ input::-webkit-outer-spin-button,
/deep/ input::-webkit-inner-spin-button {
  -webkit-appearance: none !important;
}
/deep/ input[type='number'] {
  -moz-appearance: textfield;
}
</style>